﻿using System.Data.SqlClient;
using System.Web;

namespace scmsByAspdNet.cotro.admin {
  /// <summary>
  /// getCourse 的摘要说明
  /// </summary>
  public class getCourse : IHttpHandler {

    public void ProcessRequest(HttpContext context) {
      //数据库
      SqlConnection conn = new SqlConnection("Data Source=106.54.69.143;database=scms;uid=sa;pwd=Aspdotnet666");
      conn.Open();
      //声明返回的数据
      string ret = "{\"code\":1,\"courses\":[";
      //创建SQL命令
      SqlCommand cmd = new SqlCommand("select id,title,credit,hour,attribute,proportion,faculty,comment,cover,status,is_deleted from sc_course;", conn);
      //执行SQL命令
      cmd.ExecuteNonQuery();
      //获取SqlDataReader
      SqlDataReader reader = cmd.ExecuteReader();
      //判断是否存在数据
      for (; reader.Read();) {
        if (reader[10].ToString() == "1") continue;
        ret += $"{{\"cour_id\":\"{reader[0].ToString()}\",\"title\":\"{reader[1].ToString()}\",\"credit\":\"{reader[2].ToString()}\",\"hour\":\"{reader[3].ToString()}\",\"attribute\":\"{reader[4].ToString()}\",\"proportion\":\"{reader[5].ToString()}\",\"faculty\":\"{reader[6].ToString()}\",\"comment\":\"{reader[7].ToString()}\",\"cover\":\"{reader[8].ToString()}\",\"status\":\"{reader[9].ToString()}\",\"is_deleted\":\"{reader[10].ToString()}\"}},";
      }
      ret = ret.Substring(0, ret.Length - 1);
      ret += "]}";
      conn.Close();
      //返回数据
      context.Response.StatusCode = 200;
      context.Response.Write(ret);
    }

    public bool IsReusable {
      get {
        return false;
      }
    }
  }
}